package de.Niklas.Laser.Listener;

import de.Niklas.Laser.LaserPlugin;
import de.Niklas.Laser.util.ConfigUtil.Bundle;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class AimListener implements Listener {
    LaserPlugin plugin;

    public AimListener(final LaserPlugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onAim(final PlayerInteractEvent e) {
        if (!Bundle.OPTIONS_AIMING.value) {
            return;
        }
        if (!Bundle.OPTIONS_INVERTSHOOTING.value) {
            if (e.getAction() != Action.LEFT_CLICK_AIR && e.getAction() != Action.LEFT_CLICK_BLOCK) {
                return;
            }
        } else if (e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }
        if (this.plugin.bekommeLaser().isSimilar(e.getItem())) {
            final Player p = e.getPlayer();
            if (this.plugin.imScope.contains(p.getName())) {
                this.plugin.removeScope(p);
            } else {
                this.plugin.addScope(p);
            }
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onSlotChange(final PlayerItemHeldEvent e) {
        if (!Bundle.OPTIONS_AIMING.value) {
            return;
        }
        final Player p = e.getPlayer();
        if (this.plugin.imScope.contains(p.getName())) {
            this.plugin.removeScope(p);
        }
    }

    @EventHandler
    public void onLeave(final PlayerQuitEvent e) {
        if (!Bundle.OPTIONS_AIMING.value) {
            return;
        }
        final Player p = e.getPlayer();
        if (this.plugin.imScope.contains(p.getName())) {
            this.plugin.removeScope(p);
        }
    }
}
